<template>
  <div>
     <breadcrumb></breadcrumb>
    <!-- 搜索表单 -->
    <!-- @submit.native.prevent停止默认行为 @submit是提交-->
    <el-form :inline="true" class="demo-form-inline"  @submit.native.prevent>
      <el-form-item label="姓名">
        <el-input v-model="name"  placeholder="请输入用户姓名" @keyup.native.enter="listUserByName"></el-input>
      </el-form-item>
      <el-form-item>
        <!-- 点击回车 -->
        <el-button type="primary" @click="listUserByName" >查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 分割线 -->
    <el-divider content-position="left">用户列表</el-divider>

    <!-- 表格 -->
    <template>
      <el-table
        :data="user.result"
        style="width: 100%">
        <el-table-column
          prop="uid"
          label="编号"
          align="center"
          width="100">
        </el-table-column>
        <el-table-column
          prop="uname"
          label="用户昵称"
          align="center"
          width="180"
         >
        </el-table-column>
        <el-table-column
          prop="user_name"
          label="用户真实姓名"
          align="center"
          width="180"
         >
        </el-table-column>
        <el-table-column
          prop="email"
          label="用户邮箱"
          align="center"
          width="180"
         >
        </el-table-column>
        <el-table-column
          prop="phone"
          label="电话号"
          align="center"
          width="150"
        >
        </el-table-column>
          <el-table-column  align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="success" icon="el-icon-check" @click="$router.push(`/home/user-details/${scope.row.uid}`)"  circle></el-button>
            <el-button type="primary" icon="el-icon-edit"  @click="$router.push(`/home/user-revise/${scope.row.uid}`)" circle></el-button>
            <el-button type="danger" icon="el-icon-delete" @click="DeleteItem(scope.row.uid)" circle></el-button>
            <!-- <el-button size="small" type="danger" circle icon="el-icon-delete" @click="DeleteItem(scope.row.user_id)"></el-button> -->
          </template>           
        </el-table-column>
      </el-table>
    </template>

    <br>
    <!-- 分页 -->
    <el-pagination
      background
      :total=user.total
      :page-size=user.pagesize
      layout=" ->,prev, pager, next,total,jumper"
      @current-change="ChangCurrentPage"
    >
    </el-pagination>

 
    
  </div>
</template>

<script>
import breadcrumb from "../components/breadcrumb.vue";
import tables from "../components/tables.vue";
import httpApi from "@/http";
  export default {
    components: { breadcrumb,tables },
     data(){
        return{
          name:'',
          user:{
            page: 1,
            pagesize:5,
            result:[],
            total:1
          },
          imgnone: true,
        }
     },
     methods:{
      // 删除电影列表
      DeleteItem(uid){
        // 弹窗
        this.$confirm('此操作将永久删除用户信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          
          // 确定
          httpApi.userApi.deleteUserById({uid}).then(res =>{
            if(res.data.code == 200){
               console.log("删除成功",res);
              // 提醒用户删除成功
              this.$message({ type: 'success', message: '删除成功!' });
              // 刷新
              this.search()
            }else{
              // 提醒用户删除失败
              this.$message.error('错了哦，删除失败');
            }
           
          })
        }).catch(() => {
            this.$message({ type: 'info', message: '已取消删除' });          
          });
        },

        // 点击查询按钮后执行，需要判断输入框中是否有关键字
        //trim()是去掉字符串两端的空格
        search(){
          if(this.name.trim() == ''){
            //当无输入值
            this.listuser()
          }else{
            //有输入值
            this.listUserByName()
            
          }
        },


        // 封装用户列表
        listuser(){
          // let url = "http://localhost:3000/user/list?page=1&pagesize=100"
          let params = {page: this.user.page ,pagesize: this.user.pagesize}
          httpApi.userApi.queryAllUserList(params).then(res =>{
            // console.log("用户列表",res);
            this.user = res.data.data
          })
        },
         //变化当前页
         ChangCurrentPage(page){
            // console.log("当前页数",page);
            this.user.page = page
            this.search()
        },

        // 通过昵称关键字查询演员列表
        listUserByName(){
          let params = {uname:this.name.trim(),page:this.user.page, pagesize: this.user.pagesize}
            httpApi.userApi.queryUserByNameLike(params).then(res =>{
              // console.log("用户模糊查询",res);
              this.user = res.data.data
            })     
          }
        },

      // 页面挂载完毕后，发送请求，加载列表数据
      mounted(){
        //页面挂在完毕后，发送请求，加载列表数据
        this.listuser()
      }
  }
</script>

<style lang="scss" scoped>

</style>